package com.cn.removewatermark.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.Iterator;

public class JsonUtils {

    public static String parsingJSON(String body, String key) {

        if (!JSONUtil.isJson(body)) {
            return null;
        }

        JSONObject jsonObject = JSONUtil.parseObj(body);
        Iterator<String> iterator = jsonObject.keySet().iterator();

        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.equals(key)) {
                return jsonObject.getStr(key);
            }

            Object value = jsonObject.get(next);
            if (value instanceof JSONObject) {
                String root = ((JSONObject) value).toString();
                String result = parsingJSON(root, key);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }
}
